package edu.uj.petri.model;

import edu.uj.petri.commands.Memento;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

/**
 * @author Piotrek
 */
class TransitionObjectImpl extends TransitionImpl {
    private boolean active=false;
    
    private Collection<Arc> incomingArcs;
    
    private Collection<Arc> outgoingArcs;
    
    private String name;
    
    private ObjectModel model;
    
    public TransitionObjectImpl(ObjectModel model) {
        name = "transition";
        this.model = model;
        incomingArcs = new HashSet<Arc>();
        outgoingArcs = new HashSet<Arc>();
    }
    
    public void activate() {
        active = true;
    }
    
    public void deactivate() {
        active = false;
    }
    
    public boolean isActive() {
        return active;
    }
    
    public void addIncomingArc(Arc arc) {
        incomingArcs.add(arc);
    }
    
    public void addOutgoingArc(Arc arc) {
        outgoingArcs.add(arc);
    }
    
    public void removeIncomingArc(Arc arc) {
        incomingArcs.remove(arc);
    }
    
    public void removeOutgoingArc(Arc arc) {
        outgoingArcs.remove(arc);
    }
    
    public Iterator<Arc> getIncomingArcsIterator() {
        return incomingArcs.iterator();
    }
    
    public Iterator<Arc> getOutgoingArcsIterator() {
        return outgoingArcs.iterator();
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    /** stores internal state in a memento */
    public Memento getMemento() {
        TransitionMemento retval = new TransitionMemento();
        retval.active = this.active;
        retval.name = this.name;
        retval.incomingArcs.addAll(this.incomingArcs);
        retval.outgoingArcs.addAll(this.outgoingArcs);
        return retval;
    }
    
    /** restores internal state of object to state stored in the memento */
    public void applyMemento(Memento memento) {
        if (!(memento instanceof TransitionMemento))
            throw new IllegalArgumentException("invalid memento");
        TransitionMemento tm = (TransitionMemento)memento;
        this.active = tm.active;
        this.name = tm.name;
        incomingArcs.clear();
        outgoingArcs.clear();
        incomingArcs.addAll(tm.incomingArcs);
        outgoingArcs.addAll(tm.outgoingArcs);
        model.modelChanged();
    }
    
    private static final long serialVersionUID = 1089256927491596388L;
    
    static class TransitionMemento extends Memento {
        private boolean active;
        
        private Collection<Arc> incomingArcs;
        
        private Collection<Arc> outgoingArcs;
        
        private String name;
        
        TransitionMemento() {
            incomingArcs = new HashSet<Arc>();
            outgoingArcs = new HashSet<Arc>();
        }
    }
}
